﻿using ICSharpCode.SharpZipLib.GZip;
using IDarling.BLL;
using IDarling.BLL.Parser;
using IDarling.Entity;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using System.Diagnostics;
using System.Threading;
using NCommonLib.Extensions;

namespace NPortableEngine.Test
{
    [TestClass]
    public class HttpQueryerFixture
    {
        [TestMethod]
        public async Task SimpleQueryerTest()
        {
            Dictionary<string, object> bodyDic = new Dictionary<string, object>();
            bodyDic.Add("token", "LW3vpHeB0DU=");
            bodyDic.Add("uid", "301");
            bodyDic.Add("type", "0");
            bodyDic.Add("status", "1");
            bodyDic.Add("limit", "1");
            bodyDic.Add("page", "0");

            ScouterBackData backData = await new HttpQueryer().RequestAsync(ServiceConsts.APPURL + "&mod=Public&act=showCover", HttpMethodType.POST, ReqType.JSON)
                .Params(bodyDic)
                .Decompress(null)
                .Parse(new AIDAXiuParser())
                .InMode(ReqMode.WEIGHT)
                .Fly(typeof(List<AIDAXiuEntity>));

            Assert.AreEqual(backData.IsSuccess, true);

        }

        [TestMethod]
        public async Task QueryImageTest()
        {
            ScouterBackData backData = await new HttpQueryer()
                .RequestStreamAsync("http://42.121.58.173/data/uploads/cover/282/20130101/152722/small.jpg")
                .Fly(typeof(Stream));

            Assert.IsNotNull(backData.ResponseData);
        }

        [TestMethod]
        public async Task QueryImageWithNoParser()
        {
            ScouterBackData backData = await new HttpQueryer()
                .RequestStreamAsync("http://42.121.58.173/data/uploads/cover/282/20130101/152722/small.jpg")
                .Fly(typeof(byte[]))
                .ConfigureAwait(false);

            Assert.IsNotNull(backData.ResponseData);
        }

        [TestMethod]
        public async Task QueryJsonAndDecompressResult()
        {
            Dictionary<string, object> headerDic = new Dictionary<string, object>();
            headerDic.Add("encryType", "3");
            headerDic.Add("gzipType", "0");
            headerDic.Add("Accept-Encoding", "geili-zip");
            headerDic.Add("Content-Encoding", "geili-zip");

            Dictionary<string, object> bodyDic = new Dictionary<string, object>();
            string edata = "yfiJmDlgnJNxFeE0gZXdTI5HOOlNxkrbt9xkSK4%2fy5e1A%2frK%2bgSuFGBwKEszLrxrlze54DqkyoTkTsM7C6JuxZMVYRisJEAzXKcLdF9TcsWFb4gRDNx3vfy4pK6zSCYOxZCFdG0F%2fU50OiUSAHv924F9wzKxqNlAPsKYtnlBTRSklsAl08Jke3vrQw1rzalQHXEoR9QE51XXwE7FObN4R1LEerL%2fr3akuMCJjzz3k6GM09xQ56LxRReLOO4dTu5Djgo6fAIPvc1eMKCwLYyedtszHX4xDpf3kKTm3aoUFFgg38%2f937T9kns1nQKIQXdAkW9kHQuygUQQowAJIw%2bnkElo%2bMPnkXl8YwTP%2f1V7nAwSw1xuQK9YjsQEWgLyy8TJuv7lYQZNGPXRVHtGC%2bJNVsOmjWs0FXP3Jo8U8RH2YNNqVAsS3m%2bYHqXTo2rJLrqGy2BIfN27Zwi%2f3%2fbjjiaJ2hXLhC6%2fptJSZToiXnMzXim4NVRate72PEye41rmOf3KyA6S%2b40WYFMxHJ6WoHbbCKTtKnDUCmmN%2fFukAYbRRHN%2b%2bXXqYmcOIS%2biQpaARRhqARdYP4ihKrEuZD%2bdBv0dlRzqiye6t01NLZqPaM%2fDXO7sx93tVuhMCjZI1zswY7G3PKfOqXF4wiFPfocCSp8w2b1VDwojb0G0IouO8TV9OvbK3jWpCTh%2feUhkzG3RJ%2bif";
            bodyDic.Add("edata", edata);

            ScouterBackData backData = await new HttpQueryer().RequestAsync("http://api.m.koudai.com/queryRecommendItems.do", HttpMethodType.POST, ReqType.JSON)
               .Header(headerDic)
               .Params(bodyDic)
               .Decompress(DecompressAction())
               .Parse(null)
               .InMode(ReqMode.LIGHT)
               .Fly(typeof(string));

            Debug.WriteLine("json:" + backData.ResponseData);

            Assert.AreEqual(backData.IsSuccess, true);
        }

        private Func<Stream, byte[]> DecompressAction()
        {
            return (Stream stream) =>
                {
                    using (MemoryStream resultStream = new MemoryStream())
                    {
                        using (GZipInputStream gzipStream = new GZipInputStream(stream))
                        {
                            return gzipStream.ToByteArray();
                        }
                    }
                };
        }
    }
}
